#ifndef __ST7735_TFT_H
#define __ST7735_TFT_H

#include "headfile.h"
#include "Adafruit_GFX.h"
#include "MCU_Hardware.h"

class ST7735_Base
{
    SPI *spi;
    Pin_enum cs, dc, bl, rst;

protected:
    uint8_t send_cmd(uint8_t);
    uint8_t send_data(uint8_t);

    uint8_t send_data16(uint16_t);
    uint8_t send_data16(uint16_t, int);
    uint8_t send_data16(const uint16_t *, int);

    void init();

public:
    ST7735_Base(SPI *spi_class, Pin_enum CS, Pin_enum DC, Pin_enum RST = Pin_null, Pin_enum BL = Pin_null)
    {
        spi = spi_class;
        cs = CS, dc = DC, rst = RST, bl = BL;
    }
    ~ST7735_Base(){};

    int begin();

    void clear(uint16_t color = 0xffff);
};

#include "bswap.h"
#define ST7735_RGB(r, g, b) RGB565(r, g, b)

class ST7735_GUI : public ST7735_Base, public Adafruit_GFX
{
    void setDrawArea(int x, int y, int x_e, int y_e);

public:
    ST7735_GUI(SPI *spi_class, Pin_enum CS, Pin_enum DC, Pin_enum RST = Pin_null, Pin_enum BL = Pin_null)
        : ST7735_Base(spi_class, CS, DC, RST, BL), Adafruit_GFX(128, 160)
    {
        setCursor(0, 0);
        setTextColor(0x0000);
        setTextSize(1);
    };

    virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
    virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
    virtual void fillScreen(uint16_t color);

    void showRGBImageFast(int16_t x, int16_t y, uint16_t *bitmap,
                          int16_t w, int16_t h);

    void show();
};

#endif
